import 'package:flutter/material.dart';
typedef callBack = void Function(bool isOn, String text);
class FFSwitch extends StatefulWidget {

  final String open;
  final String close;
  final Color  activityColor;
  final Color  normalColor;
  final double fontsize;
  final double width;
  final callBack onChange;

  static const Color activity = Color.fromRGBO(136, 175, 213, 1.0);

  FFSwitch({
    this.open = '开',
    this.close = '关',
    this.activityColor = activity,
    this.normalColor = Colors.white,
    this.fontsize = 12,
    this.width = 96,
    this.onChange,
  });

  @override
  State<StatefulWidget> createState() {
    return FFSwitchState();
  }

}

class FFSwitchState extends State<FFSwitch> {
  bool  isOn = true;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      width:widget.width,
      height:36,
      padding:EdgeInsets.all(2.0),
      decoration:BoxDecoration(
        borderRadius: BorderRadius.circular(18.0),
        border: Border.all(color:widget.activityColor),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children:<Widget>[
          GestureDetector(
            onTap:(){
              setState(() {
                isOn = !isOn;
                widget.onChange(isOn, isOn ? '自提' : '外送');
              });
            },
            child: Container(
              width:45,
              height: 30,
              decoration: BoxDecoration(
                color: isOn ? widget.activityColor : widget.normalColor,
                borderRadius: BorderRadius.circular(15.0),
              ),
              child:Center(
                child:Text(widget.open, style:TextStyle(fontSize:widget.fontsize, color:!isOn ? widget.activityColor : widget.normalColor))
              )
            ),
          ),

          GestureDetector(
            onTap:(){
              setState(() {
                isOn = !isOn;
                widget.onChange(isOn, isOn ? '自提' : '外送');
              });
            },
            child: Container(
              width:45,
              height: 30,
              decoration: BoxDecoration(
                color: !isOn ? widget.activityColor : widget.normalColor,
                borderRadius: BorderRadius.circular(15.0),
              ),
              child:Center(
                child:Text(widget.close, style:TextStyle(fontSize:widget.fontsize, color:isOn ? widget.activityColor : widget.normalColor))
              )
            ),
          )
        ]
      ),
    );
  }

}